var $GW2Tooltip = new function() {
  var head = document.getElementsByTagName('head')[0];
  
  this.initialize = function() {
    var style = this.createElement('link', {
                            type: "text/css",
                            href: "http://tooltips.flame-throwing.com/stylesheets/tooltip.css",
//                            href: "http://localhost:9000/stylesheets/tooltip.css",
                            rel: "stylesheet"
                        });
    head.appendChild(style);
    
    var hrefmatcher = "wiki.guildwars2.com/wiki/";
    
    var anchors = $GW2T(".gw2tooltip, a[href *= '"+hrefmatcher+"']");
    for (var i=0; i < anchors.length; i++) {
      $GW2T(anchors[i]).mouseover(this.showAtPointer);
      $GW2T(anchors[i]).mouseout(this.hideAgain);
      // need to build the tooltip as well
      var sname = anchors[i].getAttribute('ref');
      if (!sname) {
        // this was an href match
        var href = anchors[i].getAttribute('href');
        sname = unescape(href.slice(href.indexOf(hrefmatcher) + hrefmatcher.length).replace(/_/g, " "));
        anchors[i].setAttribute('ref', escape(sname));
        $GW2T(anchors[i]).addClass('gw2tooltip');
      }
      this.xdomainRequest("http://tooltips.flame-throwing.com/tooltips/"+sname+".js");
      //this.xdomainRequest("http://localhost:9000/tooltips/"+sname+".js");
    }
  }
  
  this.tooltipify = function(id, contents) {
    var tooltip = $GW2T('<div class="GW2Skills-skill-tooltip" id="gw2tooltip_'+id.replace(/[^a-zA-Z]/g, "_")+'"></div>');
    tooltip.append($GW2T(contents.tooltip_enus));
    $GW2T('body').append(tooltip);
  }
  
  this.createElement = function(type, attrs) {
    var e = document.createElement(type);
    for (var k in attrs) {
      if (attrs.hasOwnProperty(k))
        e.setAttribute(k, attrs[k]);
    }
    return e;
  }
  
  this.xdomainRequest = function (url_src) {
      var script = this.createElement('script', {
        type: "text/javascript",
        src: url_src,
        charset: "utf8"
      });
      head.appendChild(script);
  };
  
  this.showAtPointer = function(event) {
    var target = $GW2T(event.target);
    var pos = target.offset();
    if (!event.target.getAttribute('ref')) {
      target = target.parent(".gw2tooltip");
    }
    var ttip = $GW2T('#gw2tooltip_'+unescape(target.attr('ref')).replace(/[^a-zA-Z]/g, "_"));
    if (ttip.children('img.GW2Tskill_icon').size() > 0) {
      pos.left += 45;
    }
    ttip.css('top', pos.top + target.height());
    // check for right edge collide
    
    pos.left += target.width();
    if (pos.left + ttip.width() > $GW2T(window).width()) {
      pos.left = target.offset().left - ttip.width();
    }
    ttip.css('left', pos.left);
    ttip.addClass('show');
  }
  
  this.hideAgain = function(event) {
    var target = $GW2T(event.target);
    if (!event.target.getAttribute('ref')) {
      target = target.parent(".gw2tooltip");
    }
    $GW2T('#gw2tooltip_'+unescape(target.attr('ref')).replace(/[^a-zA-Z]/g, "_")).removeClass('show');
  }
  
};
var $GW2T;
(function(window, document, version, callback) {
    var j, d;
    var loaded = false;
    if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
        script.onload = script.onreadystatechange = function() {
            if (!loaded && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
                callback((j = window.jQuery).noConflict(1), loaded = true);
                j(script).remove();
            }
        };
        document.documentElement.childNodes[0].appendChild(script)
    }
}) (window, document, "1.7.1", function($, jquery_loaded) {
    $GW2T = $;
    $(document).ready(function() {
      $GW2Tooltip.initialize();
    });
});